<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="static/layui/css/layui.css">
</head>
<body>
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form  class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名:</label>
            <div class="layui-input-inline">
                <input type="text" id="qBookName" name="bookName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分类:</label>
            <div class="layui-input-inline">
                <input type="text" id="qVaries" name="varies" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <div class="layui-input-block">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-search" id="doSearch">查询</button>
            <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh">重置</button>
        </div>
    </div>
</form>

<!-- 搜索条件结束 -->

<!-- 数据表格开始 -->
<table class="layui-hide" id="userTable" lay-filter="userTable"></table>

<!--头部工具栏-->
<!--<div style="display: none;" id="userToolBar">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="add">增加</button>
    <button type="button" class="layui-btn layui-btn-sm" lay-event="batchDelete">批量删除</button>
</div>-->
<div id="userBar" style="display: none;">
    <a class="layui-btn layui-btn-xs" lay-event="yuyue">预约</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="jieyue">借阅</a>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form " lay-filter="dataFrm" id="dataFrm" method="post">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">编号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="id" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">:</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">年龄:</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh">重置</button>
            </div>
        </div>
    </form>

</div>
<!-- 添加和修改的弹出层结束 -->

<script src="static/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery', 'layer', 'form', 'table', 'laydate'], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;

        //绑定时间选择器
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });
        layer.config({
            offset: 'auto'
        });
        var tableIns;
        $(function () {
            tableIns = table.render({
                elem: '#userTable' //渲染的目标对象
                ,
                url: './book/showBook.do' //数据接口
                ,
                title: '图书信息表' //数据导出来的标题
                ,
                toolbar: "#userToolBar" //表格的工具条
                ,
                height: 'full-200',
                /*  cellMinWidth: 100 //设置列的最小默认宽度
                  ,*/
                done: function(res, curr, count) {
                    layer.msg(count);
                },
                limits:[2,4,6,8,10],
                page: true //是否启用分页

                ,
                cols: [
                    [ //列表数据
                        {
                            type: 'checkbox',
                            fixed: 'left'
                        }, {
                        field: 'bookId',
                        title: 'ID',
                        sort: true
                    }, {
                        field: 'bookName',
                        title: '书名',
                        sort: true,
                        align: 'center'
                    },{
                        field: 'author',
                        title: '作者',
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'publisher',
                        title: '出版社',
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'publishYear',
                        title: '出版年份',
                        sort: true,
                        align: 'center'
                    },{
                        field: 'stockNumber',
                        title: '库存',
                        sort: true,
                        align: 'center'
                    }, {
                            fixed: 'right',
                            title: '操作',
                            toolbar: '#userBar',
                            width: 220,
                            align: 'center'
                        }
                    ]
                ]
            })
        })
        //渲染数据表格


        //监听头部工具栏事件
        table.on("toolbar(userTable)", function(obj) {
            switch(obj.event) {
                case 'add':
                    openAddUser();
                    break;
                case 'batchDelete':
                    batchDelete();
                    break;
            };
        })

        //监听行工具事件
        table.on('tool(userTable)', function(obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'jieyue') { //借阅
                layer.msg("借阅");
                layer.confirm('确定借阅此书吗', function(index) {
                    layer.close(index);
                    //向服务端发送借阅指令
                    var bookId = data["bookId"];

                    $.ajax({
                        url: './bc/browbook.do',
                        type:"get",
                        data: {
                            "bookId": bookId
                        },
                        success: function(data) {
                            if(data.info=="SUCCESS"){
                                layer.msg("借书申请已提交，请到图书管理员处领书！！！", {
                                    icon: 1,
                                    time: 1000,
                                }, function() {
                                    window.location.reload();
                                })
                            }
                            if(data.info=="kuncun"){
                                layer.msg("当前图书库存不足，不可借阅！！！", {
                                    icon: 2,
                                    time: 1000,
                                }, function() {
                                    window.location.reload();
                                })
                            }
                            if(data.info=="num"){
                                layer.msg("你借阅次数达到上限，不可借阅！！！", {
                                    icon: 2,
                                    time: 1000,
                                }, function() {
                                    window.location.reload();
                                })
                            }

                            if(data.info=="creditScore"){
                                layer.msg("你信用积分过低，不可借阅！！！", {
                                    icon: 2,
                                    time: 1000,
                                }, function() {
                                    window.location.reload();
                                })
                            }
                        },
                        error: function() {
                            layer.msg("Ajax请求失败");
                        }
                    })

                });
            } else if(layEvent === 'yuyue') { //预约
                //openUpdateUser(data);
                var bookId = data["bookId"];
                $.ajax({
                    url: './order/apply.do',
                    type:"get",
                    data: {
                        "bookId": bookId
                    },
                    success: function(data) {

                            layer.msg("预约成功！！！", {
                                icon: 1,
                                time: 1000,
                            }, function() {
                                window.location.reload();
                            })
                    },
                    error: function() {
                        layer.msg("Ajax请求失败");
                    }
                })
            }
        });

        var url;
        var mainIndex;
        //打开添加页面
        function openAddUser() {
            mainIndex = layer.open({
                type: 1,
                title: '添加用户',
                content: $("#saveOrUpdateDiv"),
                area: ['800px', '400px'],
                success: function(index) {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    url = "user/addUser.do";
                }
            });
        }
        //批量删除方法
        function batchDelete() {
            layer.confirm('真的批量删除行么', function(index){
                var selectData = layui.table.checkStatus('userTable').data;
                var ids = new Array();
                for(var i = 0; i < selectData.length; i++) {
                    var data = selectData[i];
                    var id = data["id"];
                    ids.push(id);
                }
                $.ajax({
                    url: '<%=basePath%>/user/delUserByIds.do',
                    type:'post'
                    ,
                    data: {
                        "ids": ids
                    },
                    success: function() {
                        layer.msg("批量删除成功", {
                            icon: 1,
                            time: 1000,
                        }, function() {
                            window.location.reload();
                        })
                    },
                    error: function() {
                        layer.msg("Ajax请求失败");
                    }
                })
            });


        }
        //打开修改页面
        function openUpdateUser(data) {
            mainIndex = layer.open({
                type: 1,
                title: '借阅书籍',
                content: $("#saveOrUpdateDiv"),
                area: ['800px', '400px'],
                success: function(index) {
                    form.val("dataFrm", JSON.parse(JSON.stringify(data)));
                    /* var sex = data['sex'];
                     console.log("sex-----" + sex);
                     $("form[id='dataFrm'] input[name='sex']").each(function(i) {
                         console.log("this.value-----" + this.value);
                         if(this.value == sex) {
                             console.log(1);
                             $(this).attr("checked", "checked");
                         }
                     })*/
                    url = "user/updateUser.do";
                }
            });
        }
        //监听增加和修改的保存按钮
        form.on("submit(doSubmit)", function(obj) {
            //序列化表单数据
            var params = $("#dataFrm").serialize();
            $.post(url, params, function(obj) {
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                tableIns.reload({
                    page:{
                        curr:$(".layui-laypage-em").next().html()
                    }
                });
            })
        });
        //条件查询
        $("#doSearch").click(function(){
            // var params=$("#searchFrm").serialize();
            var bookName=$("#qBookName").val();
            var varies=$("#qVaries").val();
            tableIns.reload({
                url:"./book/showBookByTiaojian.do?",
                where: { //设定异步数据接口的额外参数，任意设
                    "bookName": bookName
                    ,"varies":varies,
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        });
    });
</script>
</body>
</html>